package lab03.test06;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
import java.util.RandomAccess;

/**
 * Created by mac on 2017/12/7.
 */
public class Test {

    public static void main(String[] args) {

        String path = "D:\\words.txt";

        RandomAccessFile randomAccessFile = null;
        Map<String,Integer> map = new HashMap<>();


        try {

            //打开一个文件
            randomAccessFile = new RandomAccessFile(path,"r");

            String line = null;

            while ((line = randomAccessFile.readLine()) != null){

                System.out.println(line);

                //1、先判断map中有没有这个单词
                if (map.get(line) == null){

                    map.put(line,1);

                }else {
                    //有这个单词怎么做
                    int value = map.get(line);
                    value = value + 1;

                    map.put(line,value);
                }

        }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }finally {

            //关闭文件
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}
